/**
 * 
 */
package com.aranin.weblog4j.services.rest;

import com.aranin.weblog4j.hashdb.HashDB;
import com.aranin.weblog4j.vo.BookList;
import com.aranin.weblog4j.vo.BookVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @author stage1
 * 
 */

public class BookService {

	protected final Logger log = LoggerFactory.getLogger(BookService.class);
	@POST
	@Path("/getbook")
	@Produces({ "application/xml" })
	@Consumes({ "application/xml"})
	public Response getBucket(BookVO bookVO) {
		log.debug("name : " + bookVO.getBookName());
		System.out.println("name" +  bookVO.getBookName());
		BookVO bookVOo = null;
		try {
			bookVOo = HashDB.getBook(URLDecoder.decode( bookVO.getBookName(), "UTF-8"));
			System.out.println("get " +  bookVO.getBookName());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace(); // To change body of catch statement use File |
									// Settings | File Templates.
		}

		if (bookVOo == null) {
			return Response.status(Response.Status.BAD_REQUEST).build();
		} else {
			return Response.ok(bookVOo).build();
		}
	}

	@POST
	@Path("/addbook")
	@Produces({ "application/xml" })
	@Consumes({ "application/xml" })
	public Response addBook(BookVO bookVO) {
		log.debug("inside addBook");
		System.out.println("l'ajout de" + bookVO.getAuthor());
		
		HashDB.insertBook(bookVO);
		
		if (HashDB.getBook(bookVO.getBookName()) == null) {
			return Response.status(Response.Status.BAD_REQUEST).build();
		} else {
			return Response.ok(bookVO).build();
			
		}
		//return Response.ok(bookVO).build();
	}
	
	@POST
	@Path("/addlistbook")
	@Produces({ "application/xml" })
	@Consumes({ "application/xml" })
	public Response addListBook(BookList bookList) {
		log.debug("inside addBook");
		BookVO bookVO = null;
		for(int i = 0 ; i < bookList.getBookList().size() ; i++)
		{
			bookVO = bookList.getBookList().get(i);
			System.out.println("l'ajout de book " + bookList.getBookList().size());
			HashDB.insertBook(bookVO);
		}
		
		if (HashDB.getBook(bookVO.getBookName()) == null) {
			return Response.status(Response.Status.BAD_REQUEST).build();
		} else {
			return Response.ok(bookList).build();
			
		}
		//return Response.ok(bookVO).build();
	}
}